<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;
use app\goods\model\SizeGroup as SizeGroupModel;
use app\goods\model\Size as SizeModel;
use app\goods\model\Brand as BrandModel;

/**
 * 尺码
 * @package app\admin\controller
 */
class Size extends Admin
{
    /**
    *尺码列表
    */
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data_list = SizeGroupModel::order(array('Sort' => 'ASC', 'SizeGroupID' => 'DESC'))->select();  

        //新增子类按钮
        
        return ZBuilder::make('table')
        ->addColumns([ // 批量添加列
            ['Sort', '排序'],
            ['SizeGroupID', 'ID'],
            ['GroupName', '名称'],
            ['Status', '状态' , 'switch'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_sizegroup')
        ->setPrimaryKey('SizeGroupID')
        ->addRightButton('custom',['title' => '新增子类', 'class' => 'btn btn-xs btn-default ' ,'icon' => 'list-icon fa fa-plus fa-fw', 'href' => url('addSize', ['id' => '__ID__'])])
        ->addRightButton('custom',['title' => '查看子类', 'class' => 'btn btn-xs btn-default ' ,'icon' => 'fa fa-fw fa-sitemap', 'href' => url('sizeList', ['id' => '__ID__'])])
        ->addRightButton('edit')
        ->addTopButton('add')
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = SizeGroupModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }


        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'GroupName', '名称'],
            ['text', 'Sort', '排序'],
            ['radio', 'Status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->fetch();
    }

    /**
    *修改
    */
    public function edit($id = 0){
        if ($id === 0) return $this->error('缺少参数');

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = SizeGroupModel::update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('修改失败');
            }
        }

        //详情
        $info      = SizeGroupModel::where('SizeGroupID',$id)->find();

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列 
            ['hidden', 'SizeGroupID'],           
            ['text', 'GroupName', '名称'],
            ['text', 'Sort', '排序'],
            ['radio', 'Status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->setFormData($info)
        ->fetch();
    }


    /**
    *新增颜色
    */
    public function addSize($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = SizeModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }

        if($id == 0){
            $this->error = '缺少参数';
        }
        //上级列表
        $groupList = SizeGroupModel::order(array('Sort' => 'ASC'))->column('SizeGroupID,GroupName');

        //品牌列表
        $brandList = BrandModel::where('Status', '1')->order(array('Sort' => 'ASC'))->column('BrandID,BrandName');

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['select', 'SizeGroupID', '所属尺码', '<span class="text-danger">必选</span>', $groupList, $id],
            ['select', 'BrandID', '品牌', '<span class="text-danger">必选</span>', $brandList],             
            ['text', 'Name', '名称'],
            ['text', 'CustomCode', '自编码'],
            ['text', 'Sort', '排序'],
            ['radio', 'Status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->layout(['SizeGroupID' => '6', 'BrandID' => '6', 'Name' => '6', 'CustomCode' => '6', 'Sort' => '6', 'Status' => '6', ])
        ->fetch();
    }


    /**
    *颜色列表
    */
    public function sizeList($id = 0){
        if($id == 0){
            $this->error('缺少参数');
        }
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data_list = SizeModel::getAll(['goods_size.SizeGroupID' => $id ],'Sort ASC,SizeID DESC'); 
        
        return ZBuilder::make('table')
        ->addColumns([ // 批量添加列
            ['Sort', '排序'],
            ['SizeID', 'ID'],
            ['GroupName', '尺码'],
            ['BrandName', '品牌'],
            ['Name', '名称'],
            ['CustomCode', '自编码'],
            ['Status', '状态' , 'switch'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_size')
        ->setPrimaryKey('SizeID')
        ->addRightButton('edit', ['href' => url('editSize', ['id' => '__id__'])])
        ->addTopButton('add', ['href' => url('addSize', ['id' => $id])])
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *修改颜色
    */
    public function editSize($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = SizeModel::update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('无修改');
            }
        }

        if($id == 0){
            $this->error = '缺少参数';
        }
        //上级列表
        $groupList = SizeGroupModel::order(array('Sort' => 'ASC'))->column('SizeGroupID,GroupName');

        //品牌列表
        $brandList = BrandModel::where('Status', '1')->order(array('Sort' => 'ASC'))->column('BrandID,BrandName');

        //详情
        $info      = SizeModel::where('SizeID', $id)->find();

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['hidden', 'SizeID'],
            ['select', 'SizeGroupID', '所属尺码', '<span class="text-danger">必选</span>', $groupList, $info['SizeGroupID']],
            ['select', 'BrandID', '品牌', '<span class="text-danger">必选</span>', $brandList, $info['BrandID']],             
            ['text', 'Name', '名称'],
            ['text', 'CustomCode', '自编码'],
            ['text', 'Sort', '排序'],
            ['radio', 'Status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->setFormData($info)
        ->layout(['SizeGroupID' => '6', 'BrandID' => '6', 'Name' => '6', 'CustomCode' => '6', 'Sort' => '6', 'Status' => '6', ])
        ->fetch();
    }


   

    
   
}
